home *** CD-ROM | disk | FTP | other *** search
/ The 640 MEG Shareware Studio 2 / The 640 Meg Shareware Studio CD-ROM Volume II (Data Express)(1993).ISO / pascal / tch_tpas.zip / TL10.TXT < prev    next >
Text File  |  1986-04-05  |  6KB  |  248 lines

  1. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 38
  2.  
  3.  
  4. TURBO-LESSON 10: WHILE STATEMENT
  5.  
  6. OBJECTIVES - In lesson 10 you will learn about:
  7.  
  8. 1. CONSTant declaration
  9. 2. WHILE statement
  10. 3. Delay timing loop
  11.  
  12. 1.  CONSTant declaration.
  13.  
  14. In the declaration section of the Pascal programs in previous 
  15. lessons, only VAR and PROGRAM have appeared.
  16.  
  17. CONST is used to declare constants and assign values to them.  
  18. The form is:
  19.  
  20. CONST  Constant_Name_1   =  Value_1;
  21.        Constant_Name_2   =  Value_2;
  22.                .
  23.                .
  24.                .
  25.        Constant_Name_n   =  Value_n;
  26.  
  27. Some examples:
  28.  
  29. CONST  PI            =  3.14159;
  30.        Fahr_Freeze   =  32;
  31.        Cels_Freeze   =  0;
  32.        Message_1     =  'This is a string constant';
  33.  
  34. ##### DO: 
  35.  
  36. Run PROG10 a time or two to see what it does.
  37.  
  38. ##### DO:
  39.  
  40. Edit the constant declaration, Wobble_Size = 5;
  41.                          to    Wobble_Size = 10;
  42.  
  43. Run the program.  What effect did you see from the change?
  44. î
  45. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 39
  46.  
  47.  
  48. ##### DO:
  49.  
  50. Change the value of Left_Edge to 20 and run the program.
  51.  
  52. To understand how Left_Edge and Wobble_Size work, look at the 
  53. WriteLn statement:
  54.  
  55. WriteLn(': ':Left_Edge,  First:Index,  Last:2,  Message);
  56.  
  57.   (1)  ': ' is printed in the right 2 columns of a field whose 
  58.        width is determined by the value of Left_Edge.
  59.  
  60.   (2) First initial is printed in the rightmost column of a field 
  61.       of width, Index.  Note that Index is changing in the FOR 
  62.       loop, so the width of this field changes.
  63.  
  64.   (3) Last initial is printed next in the rightmost column of a 
  65.       2-character wide field.
  66.  
  67.   (4) The string constant, Message, is printed immediately to the 
  68.       right of Last initial.
  69.  
  70. ##### DO:
  71.  
  72. Run the program several times with the values of the constants 
  73. reset to various values.  (LIVE DANGEROUSLY - try some wild 
  74. values:  50 for Wobble_Size or 70 for Left_Edge.  Your CRT won't 
  75. explode!  At least mine hasn't - yet!)
  76.  
  77.  
  78. 2.  WHILE statement.
  79.  
  80. The three Pascal REPETITION Structures are:
  81.   (1)  WHILE 
  82.   (2)  REPEAT
  83.   (3)  FOR
  84.  
  85. The form of the WHILE statement is:
  86.  
  87. WHILE condition DO statement;
  88.  
  89. Note that the statement is often a block statement and appears:
  90.  
  91. WHILE condition DO
  92.   BEGIN
  93.     Statement_1;
  94.     Statement_2;
  95.        .
  96.        .
  97.        .
  98.     Statement_n;
  99.   END;          
  100.  
  101. The WHILE is executed "while" the condition is true.  
  102.  
  103. Contrast the REPEAT which is executed while the condition is 
  104. false, UNTIL the condition becomes true.  
  105. î
  106. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 40
  107.  
  108.  
  109. ##### DO:
  110.  
  111. Run PROG10 again, using 0 as the number "less than 8".
  112.  
  113. How many times was the loop executed?
  114.  
  115. You could substitute REPEAT for WHILE in PROG10.
  116.  
  117. ##### DO:
  118.  
  119. Replace:     WHILE Count > 0 DO
  120.    with:     REPEAT
  121.  
  122.     add:     UNTIL Count = 0;   after:   END; {WHILE}
  123.  
  124. (Notice that you don't have to remove the BEGIN END when you 
  125. change the loop from WHILE to REPEAT.  They are not needed in 
  126. the REPEAT statement, but a block statement is always acceptable 
  127. wherever a simple statement can be used.)
  128.  
  129. Run the program several times to verify that it still works the 
  130. same as with the WHILE loop.
  131.  
  132. ##### DO:
  133.  
  134. Run the program with 0 for the "number less than 8".
  135.  
  136. How many times was the loop executed?
  137.  
  138. Notice that the WHILE loop may be executed 0 times, but the 
  139. REPEAT loop is always executed at least once, since the condition 
  140. is not checked until the end of the loop.  
  141.  
  142. NOTE: YOU SHOULD BE ON THE LOOKOUT FOR SUCH CONTRASTS BETWEEN 
  143.       "INTERCHANGEABLE" STATEMENTS.  CHOOSING THE BEST STATEMENT 
  144.       TO USE IN EACH CASE IS BASED ON THIS KIND OF AWARENESS.
  145.  
  146. ##### DO:
  147.  
  148. Change the REPEAT loop back to the original WHILE loop.
  149. (An easy way would be to load a fresh copy of the original 
  150. PROG10.) 
  151.  
  152. Change the WHILE statement to accept only Last initials less than 
  153. 'G'.
  154.  
  155. WHILE Last < 'G' DO
  156.  
  157. Run the program.   How did it work?
  158.  
  159. The WHILE loop didn't terminate at all - you had to abort the 
  160. program, right?  (If it is still executing, use Ctrl-C or Ctrl-
  161. Scroll-Lock.)
  162. î
  163. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 41
  164.  
  165.  
  166. One of the things needed in controlling loops: something in the 
  167. loop must be related to the condition.  
  168.  
  169. In the original WHILE loop, the Count was being decreased in the 
  170. loop.
  171.  
  172. When the condition was changed to (Last < 'G'), the loop no 
  173. longer has any effect on the condition.  
  174.  
  175. Either the loop will not be executed at all, if Last is = or > 
  176. 'G', or it will execute indefinitely if Last is < 'G'.  
  177.  
  178. ##### DO:
  179.  
  180. Change the WHILE statement to:
  181.  
  182. WHILE (Last < 'G') AND (Count > 0) DO
  183.  
  184. Run the program several times using last initials before and 
  185. after G in the alphabet.
  186.  
  187. ##### DO:
  188.  
  189. Run the program with last initial of 'a' (lower-case a).
  190.  
  191. How many times did the loop execute?
  192.  
  193. Why?    
  194.  
  195. The ASCII value of 'G' is 71.  The ASCII value of 'a' is 91.  So 
  196. 'a' is > 'G'.   (More on how to handle this problem in a later 
  197. lesson.)
  198.  
  199.  
  200. 3.  Delay timing loop.
  201.  
  202. You may have noticed the "wobble" proceeds faster in one 
  203. direction than the other.  This is due to a timing delay in the 
  204. first WHILE loop.  
  205.  
  206. FOR Delay := 1 to 500 DO; {Do nothing, except loop}
  207.  
  208. This looks a little strange?   There should be a statement after 
  209. the DO?
  210.  
  211. Pascal has a "null" statement for just such times as this.  When 
  212. a statement is required, but none is present, Pascal just 
  213. substitutes its "null" or "do-nothing" statement.  
  214. î
  215. TURBO-LESSONS - A Pascal Tutorial        Version 1.01    Page 42
  216.  
  217.  
  218. ##### DO:
  219.  
  220. Change the delay in the loop from 500 to 2000 and run the 
  221. program.
  222.  
  223. ##### DO:
  224.  
  225. Add a timing loop to the other WHILE loop and run the program.
  226.  
  227. How did it work?
  228.  
  229. You didn't forget to add BEGIN END when you added the delay 
  230. statement, did you?
  231.  
  232. One last little twist on timing loops:
  233.  
  234. ##### DO:
  235.  
  236. Change the Wobble_Size to 10.
  237.  
  238. Change both delay statements to:
  239.  
  240. FOR Delay := 1 to 500 * Index DO; 
  241.  
  242. Run the program.  Note that delays don't have to be constant.  
  243. This delay varies as the FOR loop progresses!
  244.  
  245.  
  246. NOTE: I HOPE YOU HAVE ENJOYED THESE FIRST 10 TURBO-LESSONS.  YOUR 
  247. COMMENTS AND SUGGESTIONS WOULD BE WELCOME!  
  248.